package view.dialog;

import view.MainFrame;
import view.panel.UserPanel;

import javax.swing.*;

import bean.User;
import dao.UserDAO;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Objects;

public class LoginDialog extends JDialog {
    private static final long serialVersionUID = 5798324533531107918L;
    private UserDAO userDAO;
    private Dimension dimension;
    private JPanel contentPane;
    private JPanel panel1;
    private JPanel panel2;
    private JPanel panel3;
    private JTextField userNameTextField;
    private JPasswordField passwordField;
    private JButton loginButton;

    public LoginDialog(Connection conn) throws ClassNotFoundException, SQLException {
        userDAO = new UserDAO(conn);
        setSize(300, 200);
        setLocationRelativeTo(null);
        setTitle("登录");
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        dimension = new Dimension(83, 22);
        contentPane = (JPanel) getContentPane();
        contentPane.setLayout(new GridLayout(5, 1));
        contentPane.add(new JPanel());
        contentPane.add(new JPanel());
        panel1 = (JPanel) contentPane.getComponent(1);
        panel1.add(new JLabel("身份"));
        userNameTextField = new JTextField();
        userNameTextField.setPreferredSize(dimension);
        panel1.add(userNameTextField);
        contentPane.add(new JPanel());
        panel2 = (JPanel) contentPane.getComponent(2);
        panel2.add(new JLabel("密码"));
        passwordField = new JPasswordField();
        passwordField.setPreferredSize(dimension);
        panel2.add(passwordField);
        contentPane.add(new JPanel());
        panel3 = (JPanel) contentPane.getComponent(3);
        loginButton = new JButton("登录");
        loginButton.addActionListener(login(conn));
        panel3.add(loginButton);
        getRootPane().setDefaultButton(loginButton);
    }
    // 添加登录按钮事件
    private ActionListener login(Connection conn) {
        return actionEvent -> {
            try {
                String userName = userNameTextField.getText();
                char[] passwordArray = passwordField.getPassword();
                String password = String.valueOf(passwordArray);
                User user = userDAO.selectByNameAndPassword(userName, password);
                if (Objects.nonNull(user)) {
                    dispose();
                    int status = user.getStatus();
                    JFrame frame = new MainFrame(conn);
                    frame.setVisible(true);
                    switch (status) {
                        case 0:
                            int userId = user.getUserId();
                            JPanel contentPane = (JPanel) frame.getContentPane();
                            JPanel cardPanel = (JPanel) contentPane.getComponent(1);
                            UserPanel userPanel = (UserPanel) cardPanel.getComponent(4);
                            userPanel.setUserId(userId);
                            break;
                        case 1:
                            contentPane = (JPanel) frame.getContentPane();
                            JPanel buttonPanel = (JPanel) contentPane.getComponent(0);
                            buttonPanel.remove(4);
                            buttonPanel.remove(3);
                            buttonPanel.remove(2);
                            cardPanel = (JPanel) contentPane.getComponent(1);
                            cardPanel.remove(4);
                            cardPanel.remove(3);
                            cardPanel.remove(2);
                            break;
                        case 2:
                            contentPane = (JPanel) frame.getContentPane();
                            buttonPanel = (JPanel) contentPane.getComponent(0);
                            buttonPanel.remove(4);
                            buttonPanel.remove(3);
                            buttonPanel.remove(1);
                            buttonPanel.remove(0);
                            cardPanel = (JPanel) contentPane.getComponent(1);
                            cardPanel.remove(4);
                            cardPanel.remove(3);
                            cardPanel.remove(1);
                            cardPanel.remove(0);
                            break;
                        default:
                            JOptionPane.showMessageDialog(this, "用户身份异常");
                            System.exit(JDialog.EXIT_ON_CLOSE);
                    }
                } else {
                    JOptionPane.showMessageDialog(this, "用户名称不存在或密码错误");
                }
            } catch (ClassNotFoundException | SQLException e) {
                e.printStackTrace();
            }
        };
    }
}
